package cn.neusoft.servlet;

import cn.neusoft.bean.Order;
import cn.neusoft.query.OrderQuery;
import cn.neusoft.service.IOrderService;
import cn.neusoft.service.impl.OrderService;
import cn.neusoft.utils.IOUtils;
import cn.neusoft.utils.JsonUtils;
import cn.neusoft.vo.ResultBean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/order")
public class OrderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("------OrdersServlet--------");

        IOrderService ordersService = new OrderService();
        String what = req.getParameter("what");

        //最终的响应体对象
        ResultBean resultBean = null;

        try {
            /*
            save表示添加或者修改：
              添加没有主键
              修改有主键
             */
            if ("save".equals(what)) {
                System.out.println("添加或修改购物车");
                //读取输入流中的json字符串(请求体的内容)
                String jsonStr = IOUtils.readStr(req.getInputStream());
                //将json字符串转成Orders对象
                Order order = JsonUtils.jsonStrToObj(jsonStr,"yyyy-MM-dd", Order.class);
                //判断Orders中有没有主键
                if(order.getOrderId()==null){
                    resultBean = ordersService.add(order);
                }else{
                    resultBean = ordersService.update(order);
                }
            } else if ("del".equals(what)) {
                //http://localhost:8080/Orders?what=del&id=1001
                System.out.println("删除餐品");
                String id = req.getParameter("id");
                resultBean = ordersService.delete(Integer.parseInt(id));
            } else if ("getById".equals(what)) {
                System.out.println("通过id查询餐品");
                String id = req.getParameter("id");
                resultBean = ordersService.getById(Integer.parseInt(id));
            } else if ("page".equals(what)) {
                System.out.println("分页搜索餐品");
                /*
                  请求体---->OrdersQuery
                  {
                    pageNow: 1,
                    pageCount: 5,
                    foodId: "小吃编号",
                  }
                 */
                String jsonStr = IOUtils.readStr(req.getInputStream());
                OrderQuery orderQuery = JsonUtils.jsonStrToObj(jsonStr, null, OrderQuery.class);
                resultBean = ordersService.getByPage(orderQuery);

            } else if("bathDel".equals(what)){

                //读取请求体的json字符串   [11,13]
                String jsonStr = IOUtils.readStr(req.getInputStream());
                //json字符串转成json数组
                JSONArray jsonArray = JSONObject.parseArray(jsonStr);
                //将json数组转成集合  集合中装着所有要删除的id值
                List<Integer> ids = jsonArray.toJavaList(Integer.class);
                System.out.println("json转集合："+ids);

                resultBean = ordersService.bathDel(ids);

            } else {
                System.out.println("无服务");
                resultBean = ResultBean.ERROR("no service");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultBean = ResultBean.ERROR(e.getMessage());
        }

        //将resultBean转成json字符串响应即可
        String resultJsonStr = JSONObject.toJSONString(resultBean);
        resp.getWriter().write(resultJsonStr);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
